package Network;

import java.io.IOException;
import java.net.*;

import Utility.LogManager;


import Context.*;
import Data.*;
/**
 * @category
 * @author Sean Lionheart
 * @version 1.0.0.0
 * This is a new thread used to send message on the sending message queue
 */
public class SendingDaemonThread extends Thread{
	
	public SendingDaemonThread(){

	}
	
	/**
	 * Override
	 */
	public void run(){
		LogManager.getSingletonManager().writeInfoLog("The sending thread starts");
		while(true){
			SendingMessage message = NetworkManager.getNetworkManager().getSendMessage();
			LogManager.getSingletonManager().writeInfoLog("Send new message");
			int defaultPort = Integer.parseInt(
					ContextHolder.getContext().getProperty(Constant.MSG_DEFUALTE_PORT));
			
			byte[] bText = message.toBuffer();
	        try {
	            DatagramPacket sendPacket = new DatagramPacket(
	                    bText, bText.length,
	                    InetAddress.getByName(message.getGuestIp()),
	                    defaultPort);
	            		NetworkManager.getNetworkManager().getMessageSocket().send(sendPacket);
	        } catch (IOException ex) {
	        }
		}
	}
}
